package com.zhongxi.guoyuan.adapter.coinShop;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.coinStore.CoinGoodsData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;

import java.util.List;

/**
 * Created by Administrator on 2016/11/24.
 */

public class CoinCategoryAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater;
    private List<CoinGoodsData> list;
    private Context context;
    public CoinCategoryAdapter(Context context, List<CoinGoodsData> data) {
        this.list = data;
        this.mLayoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.gv_item_coin_shop, parent, false);
            holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tvCoin = (TextView) convertView.findViewById(R.id.tvCoin);
            holder.tvView = (TextView) convertView.findViewById(R.id.tvPraise);
            holder.relaType = (RelativeLayout) convertView.findViewById(R.id.relaType);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        if(null!=list.get(position)&&!"".equals(list.get(position))){
            CoinGoodsData data = list.get(position);
            if(null!=data.getThumbnail()&&!"".equals(data.getThumbnail())){
                Glide.with(context).load(Url.IMG_URL+data.getThumbnail()).placeholder(R.mipmap.iv_eg).transform(new GlideRoundTransform(context, 20)).into(holder.ivImg);
            }
            if(null!=data.getName()&&!"".equals(data.getName())){
                holder.tvName.setText(data.getName());
            }else{
                holder.tvName.setText("");
            }
            if(null!=data.getPrice()&&!"".equals(data.getPrice())){
                holder.tvCoin.setText(data.getPrice());
            }else{
                holder.tvCoin.setText("");
            }
            if(null!=data.getViewnum()&&!"".equals(data.getViewnum())){
                holder.tvView.setText(data.getViewnum());
            }else{
                holder.tvView.setText("");
            }
            if(null!=data.getPaytype()&&!"".equals(data.getPaytype())){
                if("1".equals(data.getPaytype())){
                    holder.relaType.setBackgroundResource(R.color.more_orig);
                }else if("2".equals(data.getPaytype())){
                    holder.relaType.setBackgroundResource(R.color.white);
                }
            }
        }
        return convertView;
    }

    class ViewHolder{
        private TextView tvCoin;
        private TextView tvView;
        private TextView tvName;
        private ImageView ivImg;
        private RelativeLayout relaType;
    }
}
